/**
 * Created by zero on 2016/1/30.
 */
angular.module('shareservice', [])
  //第三方分享的service
  .factory('ShareService', function ($resource, $rootScope,$ionicActionSheet,ENV,Toast,Storage) {


    var APIUrl = ENV.api;
    var shareMeetingResource = $resource(APIUrl+'share/getShareMeeting.do');
    var shareHomeResource = $resource(APIUrl+'share/getShareHome.do');
    var shareSuccessResource = $resource(APIUrl+'share/getShareMeetingSuccess.do');
    //分享约局地址
    var shareMeetingUrl = ENV.api +"findMeeting/shareMeeting.do?mm_id=";
    var shareMeetingAppName = "美酒生活";

    //分享酒窝地址
    var shareHomeUrl = ENV.api + "findHome/shareHome.do?mh_id=";
    var shareHomeAppName = "美酒生活";


    var isQqInstalled;
    var isWechatInstalled;
    var isWeiBoInstalled;
    var isShowShareWindow;

    return {
      //分享约局信息给QQ好友
      shareMeetingToQQ: function (mmId) {
        shareMeetingResource.save({
          mm_id:mmId
        },function(response){
          var args = {};
          args.url = shareMeetingUrl + mmId;
          args.title = response.mm_title;
          args.description = response.mm_desc;
          args.imageUrl = ENV.picPath+response.mm_img_url;
          args.appName = shareMeetingAppName;
          YCQQ.shareToQQ(function () {
            //分享成功时调用，隐藏分享框
            Toast.showMessage("分享成功");
            var memberId = Storage.get("member").member_id;
            shareSuccessResource.save({member_id:memberId});
            return true;
          }, function (failReason) {
            Toast.showMessage("分享失败");
            return false;
          }, args);
        });
      },
      //分享约局信息到微博
      shareMeetingToWeiBo: function (mmId) {
        shareMeetingResource.save({
            mm_id:mmId
          },function(response) {
          var args = {};
          args.url = shareMeetingUrl + mmId;
          args.title = response.mm_title;
          args.description = response.mm_desc;
          args.imageUrl = ENV.picPath+response.mm_img_url;
          args.defaultText = shareMeetingAppName;
          YCWeibo.shareToWeibo(function () {
            Toast.showMessage("分享成功");
            var memberId = Storage.get("member").member_id;
            shareSuccessResource.save({member_id:memberId});
            return true;
          }, function (failReason) {
            Toast.showMessage("分享失败");
            return false;
          }, args);
        });
      },

      //分享约局信息到微信朋友
      shareMeetingToWeiXin: function (mmId) {
        shareMeetingResource.save({
            mm_id:mmId
          },function(response) {
          Wechat.share({
            message: {
              title: response.mm_title,
              description: response.mm_desc,
              mediaTagName: shareMeetingAppName,
              thumb: ENV.picPath+response.mm_img_url,
              media: {
                type: Wechat.Type.WEBPAGE,
                webpageUrl: shareMeetingUrl + mmId
              }
            },
            scene: Wechat.Scene.SESSION   // 分享到微信朋友
          }, function () {
            Toast.showMessage("分享成功");
            var memberId = Storage.get("member").member_id;
            shareSuccessResource.save({member_id:memberId});
            return true;
          }, function (reason) {
            Toast.showMessage("分享失败");
            return false;
          });
        });
      },

      //分享约局信息到微信朋友圈
      shareMeetingToWeiXinFriend: function (mmId) {
        shareMeetingResource.save({
            mm_id:mmId
          },function(response){
          Wechat.share({
            message: {
              title: response.mm_title,
              description: response.mm_desc,
              mediaTagName: shareMeetingAppName,
              thumb: ENV.picPath+response.mm_img_url,
              media: {
                type: Wechat.Type.WEBPAGE,
                webpageUrl: shareMeetingUrl + mmId
              }
            },
            scene: Wechat.Scene.TIMELINE   // 分享到朋友圈
          }, function () {
            Toast.showMessage("分享成功");
            var memberId = Storage.get("member").member_id;
            shareSuccessResource.save({member_id:memberId});
            return true;
          }, function (reason) {
            Toast.showMessage("分享失败");
            return false;
          });
        });
      },

      //分享酒窝信息给QQ好友
      shareHomeToQQ: function (mhId) {
        shareHomeResource.save({
          mh_id:mhId
        },function(response){
          //处理好链接后，放到args.url中
          var args = {};
          args.url = shareHomeUrl + mhId;
          args.title = response.share_title;
          args.description = response.share_desc;
          args.imageUrl = ENV.picPath+response.share_img;
          args.appName = shareHomeAppName;
          YCQQ.shareToQQ(function () {
            Toast.showMessage("分享成功");
            return true;
          }, function (failReason) {
            Toast.showMessage("分享失败");
            return false;
          }, args);
        });
      },

      //分享酒窝信息到微博
      shareHomeToWeiBo: function (mhId) {
        shareHomeResource.save({
            mh_id:mhId
          },function(response){
          var args = {};
          args.url = shareHomeUrl + mhId;
          args.title = response.share_title;
          args.description = response.share_desc;
          args.imageUrl = ENV.picPath+response.share_img;
          args.defaultText = shareHomeAppName;
          YCWeibo.shareToWeibo(function () {
            Toast.showMessage("分享成功");
            return true;
          }, function (failReason) {
            Toast.showMessage("分享失败");
            return false;
          }, args);
        });
      },

      //分享酒窝信息到微信朋友
      shareHomeToWeiXin: function (mhId) {
        shareHomeResource.save({
            mh_id:mhId
          },function(response){
          Wechat.share({
            message: {
              title: response.share_title,
              description: response.share_desc,
              mediaTagName: shareHomeAppName,
              thumb: ENV.picPath+response.share_img,
              media: {
                type: Wechat.Type.WEBPAGE,
                webpageUrl: shareHomeUrl + mhId
              }
            },
            scene: Wechat.Scene.SESSION   // 分享到微信朋友
          }, function () {
            Toast.showMessage("分享成功");
            return true;
          }, function (reason) {
            Toast.showMessage("分享失败");
            return false;
          });
        });
      },

      //分享酒窝信息到微信朋友圈
      shareHomeToWeiXinFriend: function (mhId) {
        shareHomeResource.save({
            mh_id:mhId
          },function(response){
          Wechat.share({
            message: {
              title: response.share_title,
              description: response.share_desc,
              mediaTagName: shareHomeAppName,
              thumb: ENV.picPath+response.share_img,
              media: {
                type: Wechat.Type.WEBPAGE,
                webpageUrl: shareHomeUrl + mhId
              }
            },
            scene: Wechat.Scene.TIMELINE   // 分享到朋友圈
          }, function () {
            Toast.showMessage("分享成功");
            return true;
          }, function (reason) {
            Toast.showMessage("分享失败");
            return false;
          });
        });
      },



      //检查手机是否安装了QQ
      checkQqIsInstalled : function(){
        YCQQ.checkClientInstalled(function(){
          isQqInstalled = true;
          isShowShareWindow = true;
          $rootScope.$broadcast('ShareService.QQInstalled');
        },function(){
          isQqInstalled = false;
          $rootScope.$broadcast('ShareService.QQInstalled');
        });
      },
      getIsQqInstalled : function () {
        return isQqInstalled;
      },
      //检查手机是否安装了微博
      checkWeiBoIsInstalled : function(){
        YCWeibo.checkClientInstalled(function(){
          isWeiBoInstalled = true;
          isShowShareWindow = true;
          $rootScope.$broadcast('ShareService.WeiBoInstalled');
        },function(){
          isWeiBoInstalled = false;
          $rootScope.$broadcast('ShareService.WeiBoInstalled');
        });
      },
      getIsWeiBoInstalled : function () {
        return isWeiBoInstalled;
      },
      //检查手机是否安装了微信
      checkWechatIsInstalled : function(){
        Wechat.isInstalled(function (installed) {
          if(installed == 1) {
            isWechatInstalled = true;
            isShowShareWindow = true;
            $rootScope.$broadcast('ShareService.WeixinInstalled');
          }else{
            isWechatInstalled = false;
            $rootScope.$broadcast('ShareService.WeixinInstalled');
          }
        }, function (reason) {
          isWechatInstalled = false;
          $rootScope.$broadcast('ShareService.WeixinInstalled');
        });
      },
      getIsWeChatInstalled : function () {
        return isWechatInstalled;
      },
      //是否显示第三方
      setIsShowShareWindow : function () {
        isShowShareWindow = true;
      },
      getIsShowShareWindow : function (){
       return isShowShareWindow;
       }
    }
  });
